package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.system.dto.MainDataCollectionPlaceDTO;
import com.ctshk.rpc.system.entity.MainDataCollectionPlace;
import com.ctshk.rpc.system.mapper.MainDataCollectionPlaceMapper;
import com.ctshk.rpc.system.req.MainDataCollectionPlaceAddReq;
import com.ctshk.rpc.system.req.MainDataCollectionPlaceIdReq;
import com.ctshk.rpc.system.req.MainDataCollectionPlaceListReq;
import com.ctshk.rpc.system.req.MainDataCollectionPlaceUpdateReq;
import com.ctshk.rpc.system.service.IMainDataCollectionPlaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 集散地點 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-10
 */
@DubboService
public class MainDataCollectionPlaceServiceImpl extends ServiceImpl<MainDataCollectionPlaceMapper, MainDataCollectionPlace> implements IMainDataCollectionPlaceService {

    @Override
    public Result add(MainDataCollectionPlaceAddReq req, TokenUser tokenUser) {
        MainDataCollectionPlace place = EntityUtil.copy(req, MainDataCollectionPlace.class);
        LocalDateTime now = LocalDateTime.now();
        long id = SnowflakeIdWorker.nextId();
        place.setId(id);
        place.setCreateId(tokenUser.getId());
        place.setModifiedId(tokenUser.getId());
        place.setModifiedName(tokenUser.getUsername());
        place.setGmtCreate(now);
        place.setGmtModified(now);
        place.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(place);
        return Result.success(id);
    }

    @Override
    public MainDataCollectionPlaceDTO query(MainDataCollectionPlaceIdReq req) {
        MainDataCollectionPlace mainDataCollectionPlace = baseMapper.selectById(req.getId());
        MainDataCollectionPlaceDTO placeDTO = EntityUtil.copy(mainDataCollectionPlace, MainDataCollectionPlaceDTO.class);
        return placeDTO;
    }

    @Override
    public Result update(MainDataCollectionPlaceUpdateReq req, TokenUser tokenUser) {
        MainDataCollectionPlace collectionPlace = EntityUtil.copy(req, MainDataCollectionPlace.class);
        LocalDateTime now = LocalDateTime.now();
        collectionPlace.setModifiedId(tokenUser.getId());
        collectionPlace.setModifiedName(tokenUser.getUsername());
        collectionPlace.setGmtModified(now);
        baseMapper.updateById(collectionPlace);
        return Result.success(req.getId());
    }

    @Override
    public PageResponse<MainDataCollectionPlaceDTO> list(MainDataCollectionPlaceListReq req) {
        Page<MainDataCollectionPlace> page = new Page<>(req.getPageNo(),req.getPageSize());
        QueryWrapper<MainDataCollectionPlace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataCollectionPlace::getIsDeleted,IsDeletedCode.NO.getCode());
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataCollectionPlace::getStatus,req.getStatus());
        //根据name模糊查询
        queryWrapper.lambda().like(StringUtils.isNotBlank(req.getName()),MainDataCollectionPlace::getAreaName,req.getName())
                .or().like(StringUtils.isNotBlank(req.getName()),MainDataCollectionPlace::getCollectionPlaceName,req.getName());
        queryWrapper.lambda().orderByDesc(MainDataCollectionPlace::getGmtModified);
        Page<MainDataCollectionPlace> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataCollectionPlace> records = iPage.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataCollectionPlaceDTO> mainDataCollectionPlaceDTOS = EntityUtil.copyList(records, MainDataCollectionPlaceDTO.class);
        return new PageResponse<>(mainDataCollectionPlaceDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    public List<MainDataCollectionPlaceDTO> queryList(Long areaId) {
        QueryWrapper<MainDataCollectionPlace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataCollectionPlace::getAreaId,areaId);
        queryWrapper.lambda().eq(MainDataCollectionPlace::getIsDeleted,IsDeletedCode.NO.getCode());
        List<MainDataCollectionPlace> mainDataCollectionPlaces = baseMapper.selectList(queryWrapper);
        List<MainDataCollectionPlaceDTO> mainDataCollectionPlaceDTOS = EntityUtil.copyList(mainDataCollectionPlaces, MainDataCollectionPlaceDTO.class);
        return mainDataCollectionPlaceDTOS;
    }


}
